﻿using System.IO;
using System.Xml.Linq;

namespace Grinder.Fb2
{
    internal static class Constants
    {
        public static XNamespace Xmlns = "http://www.gribuser.ru/xml/fictionbook/2.0";
    }

    public sealed class FictionBook
    {
        private readonly XElement _root;
        private readonly TitleInfo _titleInfo;
        private readonly DocumentInfo _documentInfo;
        private readonly PublishInfo _publishInfo;

        public TitleInfo TitleInfo { get { return _titleInfo; } }
        public DocumentInfo DocumentInfo { get { return _documentInfo; } }
        public PublishInfo PublishInfo { get { return _publishInfo; } }

        public FictionBook(string filepath)
        {
            _root = XElement.Load(filepath);

            var descriptionEl = _root.Element(Constants.Xmlns + "description");
            if (descriptionEl == null) throw new FictionBookParseException("No <description> tag found", filepath);

            var xel = descriptionEl.Element(Constants.Xmlns + "title-info");            
            _titleInfo = (xel == null) ? new TitleInfo() : new TitleInfo(xel);

            xel = descriptionEl.Element(Constants.Xmlns + "document-info");
            _documentInfo = (xel == null) ? new DocumentInfo() : new DocumentInfo(xel);

            xel = descriptionEl.Element(Constants.Xmlns + "publish-info");
            _publishInfo = (xel == null) ? new PublishInfo() : new PublishInfo(xel);
        }

        public FictionBook(FileSystemInfo file) : this(file.FullName) { }

    }
}
